package com.pingancar.creditmanage.action;

import com.pingancar.creditmanage.action.auth.UserAuthPassport;
import com.pingancar.creditmanage.pojo.*;
import com.pingancar.creditmanage.service.QueryService;
import com.pingancar.creditmanage.service.UserOrderService;
import com.pingancar.creditmanage.service.UserServiceImpl;
import com.pingancar.creditmanage.util.myenum.UserType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by kx on 2014/9/12.
 */
@Controller
@RequestMapping(value="/")
@UserAuthPassport
public class UserAction {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private QueryService queryService;
    @Autowired
    private UserOrderService userOrderService;

    public static final String LOGIN_ACTION = "/loginPage";

    @RequestMapping("/")
    public String defaultPage() {
        return "user/index";
    }

    @UserAuthPassport(validate = false)
    @RequestMapping("/loginPage")
    public String loginPage(HttpSession session) {
        session.setAttribute("url", "redirect:/");
        return "user/login";
    }

    @UserAuthPassport(validate = false)
    @RequestMapping("/registerPage")
    public String registerPage() { return "user/reg"; }

    @UserAuthPassport(validate = false)
    @RequestMapping("/register")
    public String registerUser(String mobilephone, String password, String carno, String name){
        if (userService.registerUser(mobilephone, password, carno, name)) {
            return "redirect:/loginPage";
        } else {
            return "user/reg";
        }
    }

    @UserAuthPassport(validate = false)
    @RequestMapping("/login")
    public String login(String password, String mobilephone, HttpSession session){
        UserPojo userPojo = new UserPojo();
        userPojo.setPasswd(password);
        userPojo.setMobilephone(mobilephone);
        userPojo=userService.login(userPojo);
        if (userPojo != null) {
            session.setAttribute("user", userPojo);
            session.setAttribute("type", UserType.Common);
            return "redirect:/";
        } else {
            return "redirect:/loginPage";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.setAttribute("type", UserType.Reader);
        return "redirect:/";
    }

    @RequestMapping("/settingsPage")
    public String settingPage(HttpSession session, ModelMap modelMap) {
        UserPojo userPojo = (UserPojo)session.getAttribute("user");
        List<InsurancePojo> l = userService.getInsuranceList(userPojo.getId());
        session.setAttribute("il", l);
        return "user/settings";
    }

    @RequestMapping("/addcarPage")
    public String addCarPage() {
        return "user/addcar";
    }

    @RequestMapping("/addcar")
    public String addCar(String carno, String username, HttpSession session) {
        UserPojo userPojo = (UserPojo)session.getAttribute("user");
        if (carno == null || carno == "")
            return "user/error";

        if (username != null && username != "")
            userPojo.setUsername(username);
        userPojo = userService.addCarNo(userPojo, carno);
        if (userPojo == null)
            return "user/error";

        session.setAttribute("user", userPojo);
        return "redirect:/settingsPage";
    }

    @RequestMapping("/pwPage")
    public String pwPage() {
        return "user/modifypw";
    }

    @RequestMapping("/modifyPw")
    public String modifyPw(String oldpw, String newpw, String reppw, HttpSession session) {
        if (oldpw == null || newpw == null || reppw == null || !newpw.equals(reppw))
            return "user/error";

        UserPojo userPojo = (UserPojo)session.getAttribute("user");
        userPojo = userService.modifyPw(userPojo, oldpw, newpw);
        if (userPojo == null)
            return "user/error";

        session.setAttribute("user", userPojo);
        return "redirect:/settingsPage";
    }

    @RequestMapping("/feedbackPage")
    public String feedbackPage() {
        return "user/feedback";
    }

    @RequestMapping("/addFeedback")
    public String addFeedback(FeedbackPojo feedbackPojo, HttpSession session) {
        if (feedbackPojo.getTitle() == null || feedbackPojo.getTitle().equals(""))
            return "user/error";

        UserPojo userPojo = (UserPojo)session.getAttribute("user");
        if (userService.addFeedback(userPojo, feedbackPojo))
            return "user/index";
        else
            return "user/error";
    }

    @RequestMapping("/shoplist")
    public String getShoplist(Integer type, ModelMap modelMap) {
        return "user/shoplist";
    }

    @RequestMapping("/shop")
    public String getShopDetail(Integer id, Integer type, ModelMap modelMap) {
        if (id == null)
            return "user/error";

        ShopinfoPojo shopViewPojo = queryService.getShopinfoById(id);
        if (shopViewPojo == null)
            return "user/error";

        List l = queryService.getServiceListByType(id, type);
        if (l == null || l.isEmpty())
            return "user/error";

        modelMap.addAttribute("shop", shopViewPojo);
        modelMap.addAttribute("service", l);

        return "user/shopdetail";
    }

    @RequestMapping("/addOrder")
    public String addOrder(Integer id, HttpSession session) {
        if (id == null)
            return "user/error";

        UserPojo userPojo = (UserPojo)session.getAttribute("user");
        OrderitemPojo order = userOrderService.addOrder(userPojo.getId(), id);
        if (order == null)
            return "user/error";

        session.setAttribute("user", order.getUser());

        return "redirect:/orderlist";
    }

    @RequestMapping("/orderlist")
    public String getOrderList() {
        return "user/orderlist";
    }

    @RequestMapping("/orderdetail")
    public String getOrderDetail(Integer id, HttpSession session, ModelMap modelMap) {
        if (id == null)
            return "user/error";

        OrderitemPojo orderlistPojo = queryService.getOrderlistById(id);
        if (orderlistPojo == null)
            return "user/error";

        modelMap.addAttribute("order", orderlistPojo);
        return "user/orderdetail";
    }

    @RequestMapping("/rejectorder")
    public String rejectOrder(Integer id, HttpSession session) {
        if (id == null)
            return "user/error";

        UserPojo userPojo = (UserPojo)session.getAttribute("user");
        OrderitemPojo order = userOrderService.rejectOrder(id, userPojo.getId());

        if (order == null)
            return "user/error";

        session.setAttribute("user", order.getUser());
        return "redirect:/orderlist";
    }

    @RequestMapping("/commentOrder")
    public String commentOrder(Integer id, Integer rate, String comment) {
        if (id == null || rate == null)
            return "user/error";

        if (userOrderService.commentOrder(id, rate, comment))
            return "redirect:/orderlist";

        return "user/error";
    }
}
